//
//  SLCarouselComponent.m
//  SulitLoanDemo
//
//  Created by 虞嘉伟 on 2025/2/19.
//  
  

#import "SLCarouselComponent.h"
#import <YYKit.h>
#import <Masonry.h>

@interface SLCarouselCell : UICollectionViewCell
@property (nonatomic, strong) UILabel *titleLabel;
@end
@implementation SLCarouselCell
- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self.contentView addSubview:self.titleLabel];
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView).inset(15);
            make.top.bottom.equalTo(self.contentView);
        }];
    }
    return self;
}
- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [UILabel new];
        _titleLabel.textColor = UIColorHex(#333333);
        _titleLabel.font = aspectDefaultFont(16);
    }
    return _titleLabel;
}

@end




@interface SLCarouselComponent ()<SDCycleScrollViewDelegate>
/// 还款提醒滚动到的下标
@property (nonatomic, assign) NSInteger indexOfReminder;
@end
@implementation SLCarouselComponent


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.carousel];
        [self.carousel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.top.bottom.equalTo(self);
        }];
        [self addSubview:self.moreButton];
        [self.moreButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.carousel.mas_right).offset(8);
            make.right.equalTo(self);
            make.centerY.equalTo(self.carousel);
            make.width.height.mas_equalTo(45);
        }];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.carousel.layer.cornerRadius = CGRectGetHeight(self.carousel.bounds)*0.5;
}

- (void)setStringGroup:(NSArray<NSDictionary *> *)stringGroup {
    _stringGroup = stringGroup;
    NSMutableArray *arr = [NSMutableArray array];
    for (NSDictionary *dict in stringGroup) {
        if (dict[@"laughing"]) {
            [arr addObject:dict[@"laughing"]];
        }
    }
    self.carousel.imageURLStringsGroup = arr;
}

- (Class)customCollectionViewCellClassForCycleScrollView:(SDCycleScrollView *)view {
    return [SLCarouselCell class];
}
- (void)setupCustomCell:(UICollectionViewCell *)cell forIndex:(NSInteger)index cycleScrollView:(SDCycleScrollView *)view {
    SLCarouselCell *myCell = (SLCarouselCell *)cell;
    NSDictionary *dict = self.stringGroup[index];
    if ([dict isKindOfClass:NSDictionary.class]) {
        myCell.titleLabel.text = dict[@"laughing"];
    } else {
        myCell.titleLabel.text = @"null";
    }
}

/** 图片滚动回调 */
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didScrollToIndex:(NSInteger)index {
    _indexOfReminder = index;
}

- (void)moreButtonAction:(UIButton *)button {
    !self.moreButtonCallback ?: self.moreButtonCallback(_indexOfReminder);
}


- (SDCycleScrollView *)carousel {
    if (!_carousel) {
        _carousel = [SDCycleScrollView cycleScrollViewWithFrame:CGRectZero delegate:self placeholderImage:nil];
        _carousel.scrollDirection = UICollectionViewScrollDirectionVertical;
        _carousel.showPageControl = NO;
        _carousel.layer.borderColor = UIColorHex(#1B1B1B).CGColor;
        _carousel.layer.borderWidth = 1;
        _carousel.backgroundColor = UIColor.whiteColor;
    }
    return _carousel;
}

- (UIButton *)moreButton {
    if (!_moreButton) {
        _moreButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_moreButton setImage:[UIImage imageNamed:@"btn_more_carousel"] forState:UIControlStateNormal];
        [_moreButton addTarget:self action:@selector(moreButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _moreButton;
}
@end
